﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    xmlns:user="urn:tempuri"
>
    <xsl:output method="text" indent="yes" encoding="utf-8"/>

    <xsl:param name="Namespace" select="'DAL'" />
    <xsl:param name="BaseClass" select="'System.Object'" />

    <msxsl:script language="CSharp" implements-prefix="user">
        public String Replace(String value)
        {
            return value.Replace(' ', '_');
        }
        
        public String TrimA(String value)
        {
            return value.TrimStart('@');
        }
    </msxsl:script>
    
    <xsl:template match="/">
        <xsl:for-each select="DataAdapter">
public partial class <xsl:value-of select="user:Replace(@TableName)" />DataAdapter :
    <xsl:value-of select="$BaseClass" />
 {
    public <xsl:value-of select="user:Replace(@TableName)" />DataAdapter()
    {
        Init();
    }

    #region Generated code

    public <xsl:value-of select="user:Replace(@TableName)" />DataAdapter(SqlConnection connection)
    {
        Init();
        Connection = connection;
    }

    public <xsl:value-of select="user:Replace(@TableName)" />DataAdapter(SqlConnection connection, SqlTransaction transaction) 
    {
        Init();
        Connection = connection;
        Transaction = transaction;
    }
    
<![CDATA[
    private SqlConnection connection;

    public SqlConnection Connection
    {
        get
        {
            return connection;
        }
        set
        {
            connection = value;
            SelectCommand.Connection = value;
            InsertCommand.Connection = value;
            UpdateCommand.Connection = value;
            DeleteCommand.Connection = value;
        } 
    }

    private SqlTransaction transaction;

    public SqlTransaction Transaction
    {
        get
        {
            return transaction;
        }
        set
        {
            transaction = value;
            SelectCommand.Transaction = value;
            InsertCommand.Transaction = value;
            UpdateCommand.Transaction = value;
            DeleteCommand.Transaction = value;
        }
    }

    public SqlDataAdapter Adapter
    {
        get; private set;
    }

    private SqlCommand selectCommand;

    public SqlCommand SelectCommand
    {
        get
        {
            return selectCommand;
        }
        private set
        {
            selectCommand = value;
        }
    }

    private SqlCommand insertCommand;

    public SqlCommand InsertCommand
    {
        get
        {
            return insertCommand;
        }
        private set
        {
            insertCommand = value;
        }
    }

    private SqlCommand updateCommand;

    public SqlCommand UpdateCommand
    {
        get
        {
            return updateCommand;
        }
        private set
        {
            updateCommand = value;
        }
    }

    private SqlCommand deleteCommand;

    public SqlCommand DeleteCommand
    {
        get
        {
            return deleteCommand;
        }
        set
        {
            deleteCommand = value;
        }
    }

    public virtual void Update(DataRow[] dataRows, bool smartUpdate)
    {
        if (dataRows == null)
            dataRows = new DataRow[0];

        if (!smartUpdate)
        {
            Adapter.Update(dataRows);
        }
        else
        {
            Adapter.Update(dataRows.Where(row => row.RowState == DataRowState.Deleted).ToArray());
            Adapter.Update(dataRows.Where(row => row.RowState == DataRowState.Added).ToArray());
            Adapter.Update(dataRows.Where(row => row.RowState == DataRowState.Modified).ToArray());
        }
    }

    public virtual void Update(DataRow[] dataRows)
    {
        Update(dataRows, true);
    }

    public virtual void Update(DataTable dataTable)
    {
        Update(dataTable.Rows.OfType<DataRow>().ToArray());
    }

    public virtual void Update(DataSet dataSet, String tableName)
    {
        Update(dataSet.Tables[tableName]);
    }
]]>     
    private void Init()
    {
        Adapter = new SqlDataAdapter();
        SelectCommand = new SqlCommand();
        InsertCommand = new SqlCommand();
        UpdateCommand = new SqlCommand();
        DeleteCommand = new SqlCommand();
        Adapter.SelectCommand = SelectCommand;
        Adapter.InsertCommand = InsertCommand;
        Adapter.UpdateCommand = UpdateCommand;
        Adapter.DeleteCommand = DeleteCommand; 
        
        // UpdatedRowSource
        this.InsertCommand.UpdatedRowSource = UpdateRowSource.<xsl:value-of select="./InsertCommand/@UpdatedRowSource" />;
        this.UpdateCommand.UpdatedRowSource = UpdateRowSource.<xsl:value-of select="./UpdateCommand/@UpdatedRowSource" />;
        
        SqlParameter p = null;
        // SelectCommand
        this.SelectCommand.CommandText = "<xsl:value-of select="./SelectCommand/CommandText"/>";
        <xsl:for-each select="./SelectCommand/Parameters/Parameter">
        p = SelectCommand.Parameters.Add("<xsl:value-of select="@ParameterName" />", SqlDbType.<xsl:value-of select="@SqlDbType" />);
        p.SourceColumn = "<xsl:value-of select="@SourceColumn" />";
        p.SourceVersion = DataRowVersion.<xsl:value-of select="@SourceVersion" />;</xsl:for-each>

        // InsertCommand
        this.InsertCommand.CommandText = "<xsl:value-of select="./InsertCommand/CommandText"/>";
        <xsl:for-each select="./InsertCommand/Parameters/Parameter">
        p = InsertCommand.Parameters.Add("<xsl:value-of select="@ParameterName" />", SqlDbType.<xsl:value-of select="@SqlDbType" />);
        p.SourceColumn = "<xsl:value-of select="@SourceColumn" />";
        p.SourceVersion = DataRowVersion.<xsl:value-of select="@SourceVersion" />;</xsl:for-each>

        // UpdateCommand
        this.UpdateCommand.CommandText = "<xsl:value-of select="./UpdateCommand/CommandText"/>";
        <xsl:for-each select="./UpdateCommand/Parameters/Parameter">
        p = UpdateCommand.Parameters.Add("<xsl:value-of select="@ParameterName" />", SqlDbType.<xsl:value-of select="@SqlDbType" />);
        p.SourceColumn = "<xsl:value-of select="@SourceColumn" />";
        p.SourceVersion = DataRowVersion.<xsl:value-of select="@SourceVersion" />;</xsl:for-each>

        // DeleteCommand
        this.DeleteCommand.CommandText = "<xsl:value-of select="./DeleteCommand/CommandText"/>";
        <xsl:for-each select="./DeleteCommand/Parameters/Parameter">
        p = DeleteCommand.Parameters.Add("<xsl:value-of select="@ParameterName" />", SqlDbType.<xsl:value-of select="@SqlDbType" />);
        p.SourceColumn = "<xsl:value-of select="@SourceColumn" />";
        p.SourceVersion = DataRowVersion.<xsl:value-of select="@SourceVersion" />;</xsl:for-each>
    }
    #endregion
}     
      </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
